package cn.jkgly.qrcode.controller;

import cn.jkgly.qrcode.service.ZZQR;
import cn.jkgly.utils.AuthUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 二维码相关业务
 */
@Controller
@RequestMapping("/qrcode")
public class QrCodeController {
    /**
     * 输出二维码图像数据流
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     * @throws IOException
     */
    @RequestMapping(value = {"/wxredirect"}, method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS})
    @CrossOrigin(origins = "*")
    @ResponseBody
    public String execute(HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) throws IOException {
        String uuid = httpServletRequest.getParameter("uuid");
        String appName = "bi.jkgly.cn";
        /*String encodeBackUrl = URLEncoder.encode("http://auth.jkgly.cn/auth/mobileAuthorization" +
                "?clientid=" + uuid +
                "&appName=" + appName, "utf-8");*/
        String encodeBackUrl = "http://auth.jkgly.cn/PCAuth/" + uuid +
                "/" + appName;
        /*String encodeBackUrl = "http://192.168.1.112:8080/PCAuth/" + uuid +
                "/" + appName;*/

        String encodeState = URLEncoder.encode("汉字=测试&def=123", "utf-8");
        /*String url = "https://open.weixin.qq.com/connect/oauth2/authorize" +
                "?appid=" + AuthUtil.APPID +
                "&redirect_uri=" + encodeBackUrl +
                "&response_type=code" +//固定code
                "&scope=snsapi_userinfo" +//snsapi_base(只返回openid),snsapi_userinfo
                "&state=" + encodeState +//用户自定义
                "&connect_redirect=1" + //只触发一次请求
                "#wechat_redirect";//锚点
        System.out.println(url);*/

        try {
            OutputStream os = httpServletResponse.getOutputStream();
            ZZQR.encode(encodeBackUrl, os);
            os.flush();//清空缓冲区，并将内容写到浏览器上
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }


}


